// 如果key值存在将去校对 否则直接去登录
if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            "user": get_cookie("user"),
            "key": get_cookie("key")
        },
        success: function (_res) {
            res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
                $(".content_left>ul").empty();
                var url=location.href;
                var tiao=true;
                $.each(res.data[0].power_list,function (i,o) {
                    if(url.indexOf(o.url)!==-1){
                        tiao=false;
                    }
                    var _background_=  url.indexOf(o.url)!==-1?'style="background:#000;"':"";

                    $(".content_left>ul").append('<a href="'+o.url+'"> <li '+_background_+'><span class="glyphicon '+o.icon+'"></span><p>'+o.names+'</p></li></a>');

                });

                if(tiao){
                    location.href=res.data[0].power_list[0].url;
                }
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }
        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    location.href = key_address;
}

$(function () {
    // 初始修改结束时间
    $('#set_end_time').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        singleDatePicker: true,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });
    // 初始添加结束时间
    $('#add_end_time').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        singleDatePicker: true,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });
    // 后台object
    var houtai = {
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        // 临时id存储
        linshi_id: null,
        // 临时类型存储
        queren_type: null,//0删除 1上架 2下架
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        // 检查input
        jiancha: function () {
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint($(".add_goods_body .input_is:visible").eq(i).prev().text() + " 没有填写！");
                    return false;
                }
            }
            return true;
        },
        // 限时抢购列表请求
        shop_list_ajax: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/producttime/list",
                type: "post",
                data: {
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res=JSON.parse(_res);
                    houtai.table_string(res);
                }
            });
        },
        // 抢购列表写入
        table_string: function (res) {
            // 写入table
            var string_head = "<thead><tr>  <th>抢购编号</th> <th>名称</th> <th>原价</th> <th>限时价格</th> <th>结束时间</th> <th>顺序号</th><th>状态</th> <th>操作</th></tr></thead>";
            var string_body = "";
            if (res.resStatus === 1) {
                string_body = "<tr class='text-center'><td colspan='8'>没有数据</td></tr>";
            } else if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    var down_line = o.state === 0 ? '<i class="glyphicon glyphicon-saved btn btn-info btn-xs down_line">下架</i>' : '<i class="glyphicon glyphicon-open btn btn-success btn-xs up_line">上架</i>';
                    var status = (new Date(o.endtime).getTime()) < (new Date().getTime()) ? "已过期" : "未过期";
                    var status_class = (new Date(o.endtime).getTime()) < (new Date().getTime()) ? "yiguoqi" : "weiguoqi";
                    string_body += '<tr>' +
                        '<td class="qianggou_number">' + o.id + '</td>' +
                        '<td class="qianggou_name">' + o.names + '</td>' +
                        '<td class="qianggou_price">' + o.price + '</td>' +
                        '<td class="qianggou_now_price">' + o.yes_price + '</td>' +
                        '<td class="qianggou_time">' + o.endtime + '</td>' +
                        '<td class="qianggou_xuhao">' + o.sort + '</td>' +
                        '<td class="qianggou_status ' + status_class + '">' + status + '</td>' +
                        '<td><b class="btn btn-warning btn-xs glyphicon glyphicon-tags set">修</b> <span class="glyphicon glyphicon-trash btn btn-danger btn-xs remove">删</span> ' + down_line + '</td>' +
                        '</tr>';
                });
            }

            $("#qianggou_table").empty().append(string_head + string_body);
        },
        // 添加限时抢购
        tianjia: function () {
            var end_time = new Date($("#add_end_time").val()).getTime() / 1000;
            var price = $("#add_now_price").val();
            var number = $("#add_number").val();
            var index = $("#add_index").val();
            $.ajax({
                url: ip + "index.php?s=admincms/producttime/opto",
                type: "post",
                data: {
                    tp: "add",
                    pid: number,
                    price: price,
                    endtime: end_time,
                    sort: index,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res=JSON.parse(_res);
                    console.log(res);
                    if (res.resStatus === 0) {
                     houtai.shoping_search();
                     houtai.shop_list_ajax();
                        $("#tianjia_qianggou").fadeOut(200);
                        houtai.hint("<p style='color:#0f0;'>添加" + $("#add_name").val() + "成功了</p>");
                    }else{
                        houtai.hint("添加失败了！！");
                    }

                }
            });
        },
        // 修改限时抢购
        xiugai: function () {
            var end_time = new Date($("#set_end_time").val()).getTime() / 1000;
            var price = $("#set_now_price").val();
            var number = $("#set_number").val();
            var index = $("#set_index").val();
            $.ajax({
                url: ip + "index.php?s=admincms/producttime/opto",
                type: "post",
                data: {
                    tp: "alter",
                    id: number,
                    price: price,
                    endtime: end_time,
                    sort: index,
                    token:get_cookie("key")
                },
                success: function (res) {
                    if (JSON.parse(res).resStatus === 0) {
                        houtai.shop_list_ajax();
                        houtai.hint("<p style='color:#0f0;'>修改成功了</p>");
                        $("#xiugai_qianggou").fadeOut(200);
                    }

                }
            });
        },
        // 删除限时抢购
        remove: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/producttime/opto",
                type: "post",
                data: {
                    tp: "del",
                    id: houtai.linshi_id,
                    token:get_cookie("key")
                },
                success: function (res) {
                    if (JSON.parse(res).resStatus === 0) {
                        $.each($("#qianggou_table tr .qianggou_number"), function (i, o) {
                            if ($(o).text() == houtai.linshi_id) {
                                $(o).parents("tr").remove();
                                houtai.hint("<p style='color:#0f0;'>删除" + $(o).parents("tr").find(".qianggou_name").text() + "成功了</p>");
                            }
                        });
                        $("#modal_wrap").fadeOut(200);
                    }

                }
            });
        },
        // 上架限时抢购
        up_line: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/producttime/opto",
                type: "post",
                data: {
                    tp: "show",
                    id: houtai.linshi_id,
                    token:get_cookie("key")
                },
                success: function (res) {
                    if (JSON.parse(res).resStatus === 0) {
                        $.each($("#qianggou_table tr .qianggou_number"), function (i, o) {
                            if ($(o).text() === houtai.linshi_id) {
                                $(o).parents("tr").find(".up_line")
                                    .removeClass()
                                    .addClass("glyphicon glyphicon-saved btn btn-info btn-xs down_line")
                                    .text("下架");
                                houtai.hint("<p style='color:#0f0;'>上架" + $(o).parents("tr").find(".qianggou_name").text() + "成功了</p>");
                            }
                        });
                        $("#modal_wrap").fadeOut(200);
                    }

                },
                error:function () {
                    houtai.hint("上架失败了！！");
                }
            });
        },
        // 下架限时抢购
        down_line: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/producttime/opto",
                type: "post",
                data: {
                    tp: "hide",
                    id: houtai.linshi_id,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res=JSON.parse(_res);
                    if (res.resStatus === 0) {
                        $.each($("#qianggou_table tr .qianggou_number"), function (i, o) {
                            if ($(o).text() === houtai.linshi_id) {
                                $(o).parents("tr").find(".down_line")
                                    .removeClass()
                                    .addClass("glyphicon glyphicon-open btn btn-success btn-xs up_line")
                                    .text("上架");
                                houtai.hint("<p style='color:#0f0;'>下架" + $(o).parents("tr").find(".qianggou_name").text() + "成功了</p>");
                            }
                        });
                        $("#modal_wrap").fadeOut(200);
                    }

                },
                error:function () {
                    houtai.hint("下架失败了！！");
                }
            });
        },
        // 搜索商品
        shoping_search: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/ProductTime/addList",
                type: "post",
                data: {
                    serach: $("#search_input").val(),
                    token:get_cookie("key")
                },
                success: function (res) {
                    houtai.search_table(JSON.parse(res));
                },
                error:function () {
                    houtai.hint("搜索失败！！");
                }
            });
        },
        // 写入搜索商品
        search_table: function (res) {
            var data = res.data;
            // 写入table
            var string_head = "<thead> <tr> <td>编号</td> <td>名称</td> <td>原价</td> <td>操作</td> </tr> </thead>";
            var string_body = "";
          if (res.resStatus === 0) {
                houtai.hint("<span style='color:#0f0;'>为你找到" + data.length + "条数据</span>");
                $.each(data, function (i, o) {
                    string_body += '<tr>' +
                        '<td class="linshi_number">' + o.pid + '</td>' +
                        '<td class="linshi_name">' + o.names + '</td>' +
                        '<td class="linshi_price">' + o.price + '</td>' +
                        '<td class="linshi_caozuo"><button class="glyphicon glyphicon-plus btn btn-info btn-xs add_qianggou">添加</button></td></tr>';
                });
            } else {
                string_body = "<tr class='text-center'><td colspan='4'>没有找到哦 亲！</td></tr>";
                houtai.hint("没有找到哦 亲!");
            }
            $("#linshi_table").empty().append(string_head + string_body);
            $(".table_wrap").fadeIn();
        }
    };
    // 请求抢购列表
    houtai.shop_list_ajax();

// 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });
    //添加事件
    $("#add_add").bind("click", function () {
        if (houtai.jiancha()) {
            if (houtai.click_yes()) {
                houtai.tianjia();
            }
        }
    });

    //修改事件
    $("#set_add").bind("click", function () {
        if (houtai.jiancha()) {
            if (houtai.click_yes()) {
                houtai.xiugai();
            }
        }
    });
    // 莫态框取消modal
    $("#modal_close").bind("click", function () {
        $("#modal_wrap").fadeOut(200);

    });
    // 修改取消
    $(".cancle_set").bind("click", function () {
        $(".set_shangpin").fadeOut(200);

    });
    //临时得table取消按钮
    $("#linshi_table_cancle").bind("click", function () {
        $(".table_wrap").fadeOut(200);
    });
    //抢购商品修改
    $("#qianggou_table").on("click", ".set", function () {
        houtai.linshi_id = $(this).parents("tr").find(".qianggou_number").text();
        $(".set_shangpin").hide();
        $("#xiugai_qianggou").fadeIn(200);
        $("#set_number").val($(this).parents("tr").find(".qianggou_number").text());
        $("#set_name").val($(this).parents("tr").find(".qianggou_name").text());
        $("#set_price").val($(this).parents("tr").find(".qianggou_price").text());
        $("#set_now_price").val($(this).parents("tr").find(".qianggou_now_price").text());
        $("#set_end_time").val($(this).parents("tr").find(".qianggou_time").text());
        $("#set_index").val($(this).parents("tr").find(".qianggou_xuhao").text());
    });
    //抢购商品删除s
    $("#qianggou_table").on("click", ".remove", function () {
        if ($(this).parents("tr").find(".down_line").length > 0) {
            houtai.hint('<span style="color:#2aabd2;">' + $(this).parents("tr").find(".qianggou_name").text() + '</span>已经上架不能删除');
        } else if ($(this).parents("tr").find(".down_line").length === 0) {
            $("#modal_wrap").fadeIn(200);
            $("#modal_wrap").find(".modal-body").empty().append("你确定要删除<span style='color:#f10;'>" + $(this).parents("tr").find(".qianggou_name").text() + "</span>吗？");
            houtai.linshi_id = $(this).parents("tr").find(".qianggou_number").text();
            houtai.type = 0;
        }

    });
    //抢购商品上架
    $("#qianggou_table").on("click", ".up_line", function () {
        $("#modal_wrap").fadeIn(200);
        $("#modal_wrap").find(".modal-body").empty().append("你确定要上架<span style='color:#f10;'>" + $(this).parents("tr").find(".qianggou_name").text() + "</span>吗？");
        houtai.linshi_id = $(this).parents("tr").find(".qianggou_number").text();
        houtai.type = 1;
    });
    //抢购商品下架
    $("#qianggou_table").on("click", ".down_line", function () {
        $("#modal_wrap").fadeIn(200);
        $("#modal_wrap").find(".modal-body").empty().append("你确定要下架<span style='color:#f10;'>" + $(this).parents("tr").find(".qianggou_name").text() + "</span>吗？");
        houtai.linshi_id = $(this).parents("tr").find(".qianggou_number").text();
        houtai.type = 2;
    });
    //搜索商品添加
    $("#search_all").bind("click", function () {
        if ($("#search_input").val() === "") {
            $("#search_input").focus();
            houtai.hint("请输入要添加的商品");
        } else {
            houtai.shoping_search();
        }
    });

    //回车搜索添加
    $("#form_search").submit(function () {
        if ($("#search_input").val() === "") {
            $("#search_input").focus();
            houtai.hint("请输入要添加的商品");
        } else {
            houtai.shoping_search();
        }
        return false;
    });

    //蓝色添加按钮事件
    $("#linshi_table").on("click", ".add_qianggou", function () {
        //添加临时id
        houtai.linshi_id = $(this).parents("tr").find(".linshi_number").text();
        $("#add_name").val($(this).parents("tr").find(".linshi_name").text());
        $("#add_number").val($(this).parents("tr").find(".linshi_number").text());
        $("#add_price").val($(this).parents("tr").find(".linshi_price").text());
        $("#tianjia_qianggou").fadeIn(200);
        $("#tianjia_qianggou .input_is").val("");
    });

    $("#confirm").bind("click", function () {
        if (houtai.type === 0) {
            houtai.remove();
        }
        if (houtai.type == 1) {
            houtai.up_line();
        }
        if (houtai.type == 2) {
            houtai.down_line();
        }
    });
    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
});
